/**
  * 游戏公用方法汇总
  */

module Global {
	//弹出提示消息
	export function tipShow(text: string) {
		var obj = new Object();
		obj[InstructionsEnum.TEXT] = text;
		game.AppFacade.getInstance().sendNotification(PanelNotify.HALL.OPEN_TIP, obj);
	}
	//获取时间
	export function getCurrTimeYMDHMS(hmt: boolean = true): string {
		var years;  //年份
		var months; //月份
		var datas;  //日期
		var hours;  //小时
		var minutes;//分钟
		var times;  //秒
		var timeData: string;
		//赋值
		var myDate = new Date();
		years = myDate.getFullYear();//年份
		months = myDate.getMonth() + 1;  //月份
		datas = myDate.getDate()     //日期
		hours = myDate.getHours()    //小时
		minutes = myDate.getMinutes()//分钟
		times = myDate.getSeconds(); //秒
		if (myDate.getMonth() < 9) {
			months = "0" + months;
		}
		if (myDate.getDate() < 10) {
			datas = "0" + datas;
		}
		if (hours < 10) {
			hours = "0" + hours;
		}
		if (minutes < 10) {
			minutes = "0" + minutes;
		}
		if (times < 10) {
			times = "0" + times;
		}
		if (hmt) {
			timeData = years + "-" + months + "-" + datas + " " + hours + ":" + minutes + ":" + times;
		} else {
			timeData = hours + " " + minutes;
		}
		myDate = null;
		return timeData;
	}
	//获取当前时间,返回毫秒
	export function getCurrTimeLong(): number {
		var time: string = "";
		var myDate = new Date();
		return myDate.getTime();
	}
	//时间戳转换时间
	export function Timeformat(shijianchuo) {
		function add0(m) { return m < 10 ? '0' + m : m }
		var time = new Date(shijianchuo);
		var y = time.getFullYear();
		var m = time.getMonth() + 1;
		var d = time.getDate();
		var h = time.getHours();
		var mm = time.getMinutes();
		var s = time.getSeconds();
		return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
	}
	//清空一个数组中所有的timeout
	export function removeTimeOut(list: Array<number> = InstructionsEnum.GAME_TIMEOUT_LIST) {
		if (Global.isNotEmpty(list)) {
			for (var i = 0; i < list.length; i++) {
				egret.clearTimeout(list[i]);
			}
			list.splice(0, list.length);
		}
	}
	//清空一个数组中所有的interval
	export function removeInterval(list: Array<number> = InstructionsEnum.GAME_INTERVAL_LIST) {
		if (Global.isNotEmpty(list)) {
			for (var i = 0; i < list.length; i++) {
				egret.clearInterval(list[i]);
			}
			list.splice(0, list.length);
		}
	}
	//替换所有字符，str1：被替换字符，str2：替换后的字符
	export function replaceAll(str: any, str1: any, str2: any): any {
		while (str.indexOf(str1) >= 0) {
			str = str.replace(str1, str2);
		}
		return str;
	}
	//【取随机数，范围： x~y】
	export function getRandom(n, m) {
		var random = Math.floor(Math.random() * (m - n + 1) + n);
		return random;
	}

	//【判断数字正则表达式】
	export function isNumber(val) {
		var regPos = /^\d+(\.\d+)?$/; //非负浮点数
		var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
		if (regPos.test(val) || regNeg.test(val)) {
			return true;
		} else {
			return false;
		}
	}

	//彩色分数字
	export function getmeishufen(fen: number, isrank: boolean = false) {
		var fenNum = "";
		if (fen == 0) {
			return fenNum;
		}
		if (fen > 0) {
			if (fen > 0 && !isrank) {
				fenNum += "+"
			}
			else {
				fenNum
			}
			fenNum += fen;
		} else if (fen < 0) {
			var fenArr = fen.toString().split("");
			for (var i = 0; i < fenArr.length; i++) {
				fenNum += showScore(fenArr[i]);
			}
		}
		return fenNum;
	}
	export function showScore(value: string): string {
		var fileName = "";
		switch (value + "") {
			case "-": {
				fileName = fileName + "-";
				break;
			}
			case ".": {
				fileName = fileName + "k";
				break;
			}
			case "0": {
				fileName = fileName + "j";
				break;
			}
			case "1": {
				fileName = fileName + "a";
				break;
			}
			case "2": {
				fileName = fileName + "b";
				break;
			}
			case "3": {
				fileName = fileName + "c";
				break;
			}
			case "4": {
				fileName = fileName + "d";
				break;
			}
			case "5": {
				fileName = fileName + "e";
				break;
			}
			case "6": {
				fileName = fileName + "f";
				break;
			}
			case "7": {
				fileName = fileName + "g";
				break;
			}
			case "8": {
				fileName = fileName + "h";
				break;
			}
			case "9": {
				fileName = fileName + "i";
				break;
			}
		}
		return fileName;
	}
	//判断是否为空
	export function isNotEmpty(params: any | null | undefined): boolean {
		if (params === "null"
			|| params === null
			|| params === ""
			|| params === undefined
			|| params === "undefined") {
			return false;
		} else {
			return true;
		}
	}
	//判断是否为空
	export function isEmpty(params: any): boolean {
		var relt: boolean = isNotEmpty(params);
		return (relt == true) ? false : true;
	}

	//名字长度格式化
	export function cutString(str, len = 3, a = 0) {
		//length属性读出来的汉字长度为1 
		if (a == 0) {
			if (str.length > len) {
				return str = "***" + str.substring(str.length - len, str.length);
			} else {
				return str
			}
		} else {
			if (str.length > len) {
				return str = str.substring(0, len) + "***"
			} else {
				return str
			}
		}
	}

	//获取扑克图片文件，通过对象来获取(斗地主专用)

	export function getPokerByObjectForDDZ(poker: Poker_DDZ): string {
		var fileName = "";
		poker
		if (poker.color == 0) {
			return fileName + "ddz_poker_0_" + poker.value;
		}
		if (poker.color == 1) {
			return fileName + "ddz_poker_1_" + poker.value;
		}
		if (poker.color == 2) {
			return fileName + "ddz_poker_2_" + poker.value;
		}
		if (poker.color == 3) {
			return fileName + "ddz_poker_3_" + poker.value;
		}
		if (poker.color == 4) {
			return fileName + "ddz_poker_4_" + poker.value;
		}
	}
	//获取扑克图片文件，通过对象来获取（其他游戏公用）
	export function getPokerByObject(poker: Poker_public): string {
		var fileName = "";
		poker
		if (poker.color == 0) {
			return fileName + "poker_0_" + poker.value;
		}
		if (poker.color == 1) {
			return fileName + "poker_1_" + poker.value;
		}
		if (poker.color == 2) {
			return fileName + "poker_2_" + poker.value;
		}
		if (poker.color == 3) {
			return fileName + "poker_3_" + poker.value;
		}
		if (poker.color == 4) {
			return fileName + "poker_4_" + poker.value;
		}
	}
	//获取扑克图片文件，通过ID来获取（其他游戏公用）
	export function getPokerById(id): string {
		var fileName = "";
		var ID = parseInt(id);
		if (ID > 0 && ID <= 13) {
			return fileName + "poker_0_" + (id - 0);
		} else if (ID > 16 && ID <= 29) {
			return fileName + "poker_1_" + (ID - 16);
		} else if (ID > 32 && ID <= 45) {
			return fileName + "poker_2_" + (ID - 32);
		} else if (ID > 48 && ID <= 61) {
			return fileName + "poker_3_" + + (ID - 48);
		}
	}

	//获取静态密匙  //支付 查游戏记录等接口用
	export function getSynLoginKey(time: number): string {
		var md5Str = GameVO.getVo().dwUserID + GameVO.getVo().szDynamicPass + time + InstructionsEnum.SyncLoginKey;
		var key = new utils.MD5().hex_md5(md5Str)
		key = key.toUpperCase()
		return key;
	}
	//js保留两位小数，不够自动补充0
	export function returnFloat(value) {
		if (value.length > 15) {
			value = value.substring(0, 15)
		}
		value = Math.floor(parseFloat(value) * 100) / 100;
		var xsd = value.toString().split(".");
		if (xsd.length == 1) {
			value = value.toString() + ".00";
			return value;
		}
		if (xsd.length > 1) {
			if (xsd[1].length < 2) {
				value = value.toString() + "0";
			}
			return value;
		}
	}
	//秒转分钟
	export function s_to_hs(s) {
		//计算分钟
		//算法：将秒数除以60，然后下舍入，既得到分钟数
		var h;
		h = Math.floor(s / 60);
		s = s % 60;
		var text = ""
		if (h != 0) {
			text += h + '分钟'
		}
		text += s + "秒"
		return text;
	}
}	